<?php
/**
 * Main controller class which calls other controllers
 *
 * @author zonk
 */
class MainController {

        /**
         * @var Array with request data from HTTP-Header and url
         */
        private $request = null;

        /**
         * The current user using this application
         * @var UserModel
         */
        public $user;

        /**
         * @var string Template name
         */
        private $template = '';

        /**
         *
         * @var View The main view which contains the subviews
         */
        private $view = null;

        function __construct($request){
                session_start();
                // If session is started and userid is give fetch the user from the db
                if(!empty($_SESSION['userid'])){
                        $this->user = UserModel::getUserById($_SESSION['userid']);
                }
                // Else make a new user(has guest permission)
                else {
                        $this->user = new UserModel(null, 'Guest', 'Guest', 'none', 'none', 'none', 'none', 0, 0, null, null, null, null, null);
                }
                $this->view = new View($this->getUser());
                $this->request = $request;
                $this->template = !empty($request['view']) ? $request['view'] : 'default';
        }

        /**
         * Sets the current user of this application
         * @param UserModel $user
         */
        public function setUser(UserModel $user){
                $this->user = $user;
        }

        /**
         * Returns the user who is currently using this programm
         * @return UserModel
         */
        public function getUser(){
                return $this->user;
        }

        /**
         * Returns the request
         * @return array
         */
        public function getRequest(){
                return $this->request;
        }

        /**
         * Function to make the controller display his view
         */
        function display(){
                // This is the MainController so the layout needs to be done here
                $this->view->setTemplate('Layout');
                $this->view->setPermission(Permission::USER);
                $headerView = new View($this->getUser());
                $headerView->setPermission(Permission::USER);
                $headerView->setTemplate('Header');
                $this->view->assign('header', $headerView->loadTemplate());
                $navi = new NavigationController($this);
                $this->view->assign('navigation', $navi->display());
                $loginfieldView = new View($this->getUser());
                $loginfieldView->setPermission(Permission::GUEST);
                $loginfieldView->setTemplate('Loginpage');
                $this->view->assign('loginpage', $loginfieldView->loadTemplate());
                $footerView = new View($this->getUser());
                $footerView->setPermission(Permission::USER);
                $footerView->setTemplate('Footer');
                $this->view->assign('footer', $footerView->loadTemplate());
                $contentView = new View($this->getUser());
                // Subcontrollers are called which are responsable for the views
                switch($this->template){
                        case 'storageDevices':
                                $contentController = new StorageDevicesController($this);
                                break;
                        case 'recommendations':
                        		$contentController = new RecommendationsController($this);
                        		break;
                        case 'profile':
                                $contentController = new ProfileController($this);
                                break;
                        case 'admin':
                                $contentController = new AdminController($this);
                                break;
                        default:
                                $contentController = new MovieController($this);
                }
                $this->view->assign('content', $contentController->display());
                return $this->view->loadTemplate();
        }
}
?>